package org.spitter.web.controller;

import javax.inject.Inject;
import javax.validation.Valid;

import org.spitter.core.model.Spitter;
import org.spitter.core.service.SpitterService;
import org.spitter.core.service.SpittleService;
import org.spitter.util.ImageUploadException;
import org.spitter.util.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/spitters")
public class SpitterController {
	private final SpitterService spitterService;
	private final SpittleService spittleService;

	@Inject
	public SpitterController(SpitterService spitterService,
			SpittleService spittleService) {
		this.spitterService = spitterService;
		this.spittleService = spittleService;
	}

	/*
	 * Shows the spittles of a spitter !!!!!!!!!!
	 */
	@RequestMapping(value = "/spittles", method = RequestMethod.GET)
	public String listSpittlesForSpitter(
			@RequestParam("spitter") String username, Model model) {
		Spitter spitter = spitterService.getByUsername(username);
		model.addAttribute(spitter);
		model.addAttribute(spittleService.getSpittlesForSpitter(spitter));

		return "spittles/list";
	}

	/*
	 * Shows the spitter register form
	 */
	@RequestMapping(method = RequestMethod.GET, params = "new")
	public String createSpitterProfile(Model model) {
		model.addAttribute(new Spitter());

		return "spitters/edit";
	}

	/*
	 * Controller for registering a spitter
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String addSpitterFromForm(@Valid Spitter spitter,
			BindingResult bindingResult,
			@RequestParam(value = "image", required = false) MultipartFile image) {
		if (bindingResult.hasErrors()) {
			return "spitters/edit";
		}

		ImageUtil imageUtil = new ImageUtil();
		try {
			if (image.isEmpty()) {
				spitterService.save(spitter);

			} else {
				imageUtil.validateImage(image);
				spitterService.save(spitter);

				String extension = image.getOriginalFilename().substring(
						image.getOriginalFilename().lastIndexOf("."));
				imageUtil.saveProfileImage(spitter.getId() + extension, image);
			}
		} catch (ImageUploadException e) {
			bindingResult.reject(e.getMessage());
			return "spitters/edit";
		}

		return "redirect:/spitters/" + spitter.getUsername();
	}

	/*
	 * Controller for showing a spitter's profile page
	 */
	@RequestMapping(value = "/{username}", method = RequestMethod.GET)
	public String showSpitterProfile(@PathVariable("username") String username,
			Model model) {
		Spitter spitter = spitterService.getByUsername(username);
		if (spitter != null) {
			model.addAttribute(spitter);
			model.addAttribute(spittleService.getSpittlesForSpitter(spitter));
			return "spitters/view";
		} else {
			return "redirect:/";
		}
	}
	
	
	/*
	 * 
	 * RESTful CRUD operations on Spitters 
	 * 
	 */
	
	
	
	/*
	 * 
	 * CRUD operation on Spittles for a spitter using REST/JSON
	 * 
	 */
}
